(function() {
	// trim polyfill : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
	if(!String.prototype.trim) {
		(function() {
			// Make sure we trim BOM and NBSP
			var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
			String.prototype.trim = function() {
				return this.replace(rtrim, '');
			};
		})();
	}

	[].slice.call(document.querySelectorAll('input.input__field')).forEach(function(inputEl) {
		// in case the input is already filled..
		if(inputEl.value.trim() !== '') {
			classie.add(inputEl.parentNode, 'input--filled');
		}

		// events:
		inputEl.addEventListener('focus', onInputFocus);
		inputEl.addEventListener('blur', onInputBlur);
	});

	function onInputFocus(ev) {
		classie.add(ev.target.parentNode, 'input--filled');
	}

	function onInputBlur(ev) {
		if(ev.target.value.trim() === '') {
			classie.remove(ev.target.parentNode, 'input--filled');
		}
	}
})();

$(function() {
	$('#login #login-password').focus(function() {
		$('.login-owl').addClass('password');
	}).blur(function() {
		$('.login-owl').removeClass('password');
	});
	$('#login #register-password').focus(function() {
		$('.register-owl').addClass('password');
	}).blur(function() {
		$('.register-owl').removeClass('password');
	});
	$('#login #register-repassword').focus(function() {
		$('.register-owl').addClass('password');
	}).blur(function() {
		$('.register-owl').removeClass('password');
	});
	$('#login #forget-password').focus(function() {
		$('.forget-owl').addClass('password');
	}).blur(function() {
		$('.forget-owl').removeClass('password');
	});
});

function goto_register() {
	$("#register-username").val("");
	$("#register-password").val("");
	$("#register-repassword").val("");
	$("#register-code").val("");
	$("#tab-2").prop("checked", true);
}

function goto_login() {
	$("#login-username").val("");
	$("#login-password").val("");
	$("#tab-1").prop("checked", true);
}

function goto_forget() {
	$("#forget-username").val("");
	$("#forget-password").val("");
	$("#forget-code").val("");
	$("#tab-3").prop("checked", true);
}

function login() { //登录
	var username = $("#login-username").val(),
		password = $("#login-password").val(),
		verifycode = $("#login-verify-code").val(),
		validatecode = null;
	//判断用户名密码是否为空
	if(username == "") {
		$.pt({
			target: $("#login-username"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "用户名不能为空"
		});
		return;
	}
	if(password == "") {
		$.pt({
			target: $("#login-password"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "密码不能为空"
		});
		return;
	}
	if(verifycode == "") {
		$.pt({
			target: $("#login-verify-code-canvas"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "验证码不能为空"
		});
		return;
	}
	if(timeout_flag) {
		$.pt({
			target: $("#login-verify-code-canvas"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "验证码已经失效"
		});
		return;
	}
	//判断验证码是否正确
	if(verifycode != show_num.join("")) {
		$.pt({
			target: $("#login-verify-code-canvas"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "验证码不正确"
		});
		return;
	}

	//登录
	//调用后台登录验证的方法
	$.ajax({
		type: "post",
		url: "http://localhost:8081/user/login",
		data: {
			userUserName: username,
			userPassword: password
		},
		dataType: "json",
		success: function(data) {
			if(data.code == 0) {
				window.location.href = "learn-HomePage.html";
			} else {
				data.code == 1
			} {
				alert(data.msg);
			}
		},
		error: function() {
			alert("发送失败了~请稍后重试一下>_<");
		}
	});
	return false;
}

//手机发送验证码
function forgetMessage() {
	//获取手机号
	let phone = $("#forget-phone").val(),
		msg_flag = false,
		username = $("#forget-username").val();
	let regExp = new RegExp("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0135678])|(18[0-9]))\\d{8}$");
	if(!regExp.test(phone)) {
		$.pt({
			target: $("#forget-phone"),
			position: 'r',
			align: 't',
			width: 'auto',
			height: 'auto',
			content: "请输入正确的手机格式"
		});
		msg_flag = true;
	}
	if(msg_flag) {
		return false;
	} else {
		//先使用用户名发送ajax请求--查看该用户是否存在
		$.ajax({
			type: "post",
			url: "http://localhost:8081/user/userPhoneMsg",
			data: {
				userUserName: username,
				userPhone: phone
			},
			dataType: "json",
			success: function(data) {
				//返回的状态值为0说明查询成功账号存在
				if(data.code == 0) {
					alert(data.msg);
				} else if(data.code == 1) {
					msg("#forget-username", data.msg);
				} else if(data.code == 2 || data.code == 3) {
					msg("#forget-phone", data.msg);
				}
			},
			error: function() {
				alert("发送失败了~请稍后重试一下>_<");
			}
		});

	}
}

//重置密码
function forget() {
	//获取用户名
	var username = $("#forget-username").val(),
		//获取重置的密码
		password = $("#forget-password").val(),
		//获取手机号
		phone = $("#forget-phone").val();
	//获取验证码
	code = $("#forget-code").val(),
		flag = false;
	//判断用户名密码是否为空
	if(username == "") {
		msg("#forget-username", "用户名不能为空");
		flag = true;
	}
	//判断密码是否为空
	if(password == "") {
		msg("#forget-password", "密码不能为空");
		flag = true;
	}
	if(code == "") {
		msg("#forget-code", "请输入验证码");
		flag = true;
	}
	if(phone == "") {
		msg("#forget-phone", "请输入手机号,并发送验证码");
		flag = true;
	}
	//用户名只能是15位以下的字母或数字
	let regExp = new RegExp("^[a-zA-Z0-9_]{1,15}$");
	if(!regExp.test(username)) {
		msg("#forget-username", "用户名必须为15位以下的字母或数字");
		flag = true;
	}
	//密码必须要有数字和字母,且最少不能少于六位
	let regExpa = new RegExp("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$");
	if(!regExpa.test(password)) {
		msg("#forget-password", "密码必须要有数字和字母,且最少不能少于六位");
		flag = true;
	}
	if(flag) {
		return false;
	} else { //重置密码
		//对比验证码输入是否正确,和密码的验证
		$.ajax({
			type: "post",
			url: "http://localhost:8081/user/passwordReset",
			data: {
				userUserName: username,
				userPassword: password,
				userPhone: phone,
				code: code
			},
			dataType: "json",
			success: function(data) {
				if(data.code != 0) {
					alert(data.msg)
				} else {
					spop({
						template: '<h4 class="spop-title">重置密码成功</h4>即将于3秒后返回登录',
						position: 'top-center',
						style: 'success',
						autoclose: 3000,
						onOpen: function() {
							var second = 2;
							var showPop = setInterval(function() {
								if(second == 0) {
									clearInterval(showPop);
								}
								$('.spop-body').html('<h4 class="spop-title">重置密码成功</h4>即将于' + second + '秒后返回登录');
								second--;
							}, 1000);
						},
						onClose: function() {
							goto_login();
						}
					});
				}
			}
		});

		return false;
	}
}

/**
 * 提示框 
 * @param {Object} key 提示框标签id
 * @param {Object} value 提示内容
 */
function msg(key, value) {
	$.pt({
		target: $(key),
		position: 'r',
		align: 't',
		width: 'auto',
		height: 'auto',
		content: value
	});
}